package com.dailyblue.java.standard.network.bio;

import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class 发送 extends Thread {

    private OutputStream out;
    private boolean type; // true:server false:clint
    private Scanner scanner;

    public 发送(OutputStream out, boolean type) {
        this.out = out;
        this.type = type;
        scanner = new Scanner(System.in);
    }

    @Override
    public void run() {
        while (true) {
            String message = scanner.next();
            message = (type ? "服务器：" : "客户端：") + message;
            try {
                out.write(message.getBytes(StandardCharsets.UTF_8));
                out.flush();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if ("exit".equals(message)) {
                break;
            }
        }
    }
}
